function _typeof(obj) {
  '@babel/helpers - typeof';
  return (
    (_typeof =
      'function' == typeof Symbol && 'symbol' == typeof Symbol.iterator
        ? function (obj) {
            return typeof obj;
          }
        : function (obj) {
            return obj &&
              'function' == typeof Symbol &&
              obj.constructor === Symbol &&
              obj !== Symbol.prototype
              ? 'symbol'
              : typeof obj;
          }),
    _typeof(obj)
  );
}
var _excluded = [
  'when',
  'name',
  'label',
  'max',
  'min',
  'hiddenTopLabel',
  'className',
  'style',
  'header',
  'footer',
  'renderListItemsAddonBefore',
  'renderListItemsAddon',
  'renderListItemByFields',
  'renderListItem',
  'renderListFields',
  'onAddPre',
  'children',
];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    enumerableOnly &&
      (symbols = symbols.filter(function (sym) {
        return Object.getOwnPropertyDescriptor(object, sym).enumerable;
      })),
      keys.push.apply(keys, symbols);
  }
  return keys;
}
function _objectSpread(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = null != arguments[i] ? arguments[i] : {};
    i % 2
      ? ownKeys(Object(source), !0).forEach(function (key) {
          _defineProperty(target, key, source[key]);
        })
      : Object.getOwnPropertyDescriptors
      ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
      : ownKeys(Object(source)).forEach(function (key) {
          Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
  }
  return target;
}
function _defineProperty(obj, key, value) {
  key = _toPropertyKey(key);
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true,
    });
  } else {
    obj[key] = value;
  }
  return obj;
}
function _toPropertyKey(arg) {
  var key = _toPrimitive(arg, 'string');
  return _typeof(key) === 'symbol' ? key : String(key);
}
function _toPrimitive(input, hint) {
  if (_typeof(input) !== 'object' || input === null) return input;
  var prim = input[Symbol.toPrimitive];
  if (prim !== undefined) {
    var res = prim.call(input, hint || 'default');
    if (_typeof(res) !== 'object') return res;
    throw new TypeError('@@toPrimitive must return a primitive value.');
  }
  return (hint === 'string' ? String : Number)(input);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import MinusCircleOutlined from '@ant-design/icons/MinusCircleOutlined';
import PlusOutlined from '@ant-design/icons/PlusOutlined';
import { Form as AntdForm, Button, message } from 'antd';
import 'antd/es/button/style/index.js';
import 'antd/es/form/style/index.js';
import 'antd/es/message/style/index.js';
import classNames from 'classnames';
import React, { useCallback } from 'react';
import { useFormAttrsContext } from '../../_utils/contexts/FormAttrsContext';
import FormItem from '../../FormItem';
import './index.less';
/**
 * 便捷的动态增减表单
 * @returns
 */
function FieldList(_ref) {
  var when = _ref.when,
    name = _ref.name,
    label = _ref.label,
    max = _ref.max,
    min = _ref.min,
    hiddenTopLabel = _ref.hiddenTopLabel,
    className = _ref.className,
    style = _ref.style,
    header = _ref.header,
    footer = _ref.footer,
    renderListItemsAddonBefore = _ref.renderListItemsAddonBefore,
    renderListItemsAddonProp = _ref.renderListItemsAddon,
    renderListItemByFields = _ref.renderListItemByFields,
    renderListItemProp = _ref.renderListItem,
    renderListFields = _ref.renderListFields,
    onAddPre = _ref.onAddPre,
    children = _ref.children,
    rest = _objectWithoutProperties(_ref, _excluded);
  var fieldListName = name;
  var _useFormAttrsContext = useFormAttrsContext(),
    detail = _useFormAttrsContext.detail;

  // 渲染增减表单项
  var renderListItem = useCallback(
    function (listField, operators, options) {
      var key = listField.key,
        name = listField.name,
        isListField = listField.isListField;
      var remove = operators.remove;
      var listFields = options.listFields;
      if (renderListItemProp) {
        return /*#__PURE__*/ React.createElement(
          'div',
          {
            key: key,
            className: 'szzj-field-list-item',
          },
          renderListItemProp(listField, operators, options),
        );
      }
      if (!children) return;

      // 表单控件
      var fields = /*#__PURE__*/ React.createElement(
        'div',
        {
          className: 'szzj-field-list-item-fields',
        },
        typeof children === 'function'
          ? children({
              name: name,
              isListField: isListField,
            })
          : Array.isArray(children)
          ? children.map(function (child, index) {
              if (typeof child === 'function') {
                var childElem = child({
                  name: name,
                  isListField: isListField,
                  fieldListName: fieldListName,
                });
                return /*#__PURE__*/ React.cloneElement(childElem, {
                  key: index,
                });
              } else {
                var _child$props$key;
                // @todo 校验 name 未设置时，children 数量只能为一
                return /*#__PURE__*/ React.cloneElement(
                  child,
                  _objectSpread(
                    _objectSpread({}, child.props),
                    {},
                    {
                      name: child.props.name ? [name, child.props.name] : name,
                      isListField: isListField,
                      key:
                        (_child$props$key = child.props.key) !== null && _child$props$key !== void 0
                          ? _child$props$key
                          : child.props.name,
                    },
                  ),
                );
              }
            })
          : React.Children.map(children, function (child) {
              var _child$props$key2;
              // @todo 校验 name 未设置时，children 数量只能为一
              return /*#__PURE__*/ React.cloneElement(
                child,
                _objectSpread(
                  _objectSpread({}, child.props),
                  {},
                  {
                    name: child.props.name ? [name, child.props.name] : name,
                    isListField: isListField,
                    key:
                      (_child$props$key2 = child.props.key) !== null && _child$props$key2 !== void 0
                        ? _child$props$key2
                        : child.props.name,
                  },
                ),
              );
            }),
      );
      if (renderListItemByFields) {
        return /*#__PURE__*/ React.createElement(
          'div',
          {
            key: key,
            className: 'szzj-field-list-item',
          },
          renderListItemByFields(
            fields,
            _objectSpread(
              _objectSpread(_objectSpread({}, operators), options),
              {},
              {
                listField: listField,
              },
            ),
          ),
        );
      }
      return /*#__PURE__*/ React.createElement(
        'div',
        {
          key: key,
          className: 'szzj-field-list-item',
        },
        /*#__PURE__*/ React.createElement(
          'div',
          {
            className: 'szzj-field-list-item-header',
          },
          /*#__PURE__*/ React.createElement('span', null, ''.concat(label).concat(name + 1)),
          (!min || listFields.length > min) &&
            !detail &&
            /*#__PURE__*/ React.createElement(
              'span',
              {
                className: 'szzj-field-list-remove',
                onClick: function onClick() {
                  return remove(name);
                },
              },
              /*#__PURE__*/ React.createElement(MinusCircleOutlined, {
                className: 'szzj-field-list-remove-icon',
              }),
              '\u5220\u9664',
            ),
        ),
        fields,
      );
    },
    [renderListItemProp, renderListItemByFields],
  );
  var renderListItemsAddon = useCallback(
    function (options) {
      if (renderListItemsAddonProp) return renderListItemsAddonProp(options);
      var listFields = options.listFields,
        max = options.max,
        detail = options.detail,
        add = options.add,
        errors = options.errors;
      if (!max || (listFields.length < max && !detail)) {
        return /*#__PURE__*/ React.createElement(
          React.Fragment,
          null,
          /*#__PURE__*/ React.createElement(
            Button,
            {
              type: 'dashed',
              className: 'szzj-field-list-add',
              onClick: function onClick() {
                if (onAddPre) {
                  try {
                    var result = onAddPre({
                      index: listFields.length,
                    });
                    add(result);
                  } catch (err) {
                    var _err$message;
                    message.error(
                      (_err$message = err.message) !== null && _err$message !== void 0
                        ? _err$message
                        : '添加失败',
                    );
                  }
                } else {
                  add();
                }
              },
              block: true,
              icon: /*#__PURE__*/ React.createElement(PlusOutlined, null),
            },
            '\u6DFB\u52A0',
            label !== null && label !== void 0 ? label : '',
          ),
          /*#__PURE__*/ React.createElement(AntdForm.ErrorList, {
            errors: errors,
          }),
        );
      }
      return null;
    },
    [renderListItemsAddonProp, onAddPre],
  );
  return /*#__PURE__*/ React.createElement(
    FormItem,
    {
      label: label,
      when: when,
      className: classNames('szzj-field-list-wrapper', {
        'hidden-top-label': hiddenTopLabel,
      }),
    },
    /*#__PURE__*/ React.createElement(
      'div',
      {
        className: classNames('szzj-field-list', className),
        style: style,
      },
      header &&
        /*#__PURE__*/ React.createElement(
          'div',
          {
            className: 'szzj-field-list-header',
          },
          header,
        ),
      /*#__PURE__*/ React.createElement(
        AntdForm.List,
        _extends(
          {
            name: name,
          },
          rest,
        ),
        function (listFields, _ref2, _ref3) {
          var add = _ref2.add,
            remove = _ref2.remove,
            move = _ref2.move;
          var errors = _ref3.errors,
            warnings = _ref3.warnings;
          var listFieldsNode = renderListFields
            ? renderListFields({
                name: name,
                listFields: listFields,
                add: add,
                remove: remove,
                move: move,
                errors: errors,
                warnings: warnings,
              })
            : listFields.map(function (listField) {
                return renderListItem(
                  listField,
                  {
                    add: add,
                    remove: remove,
                    move: move,
                  },
                  {
                    listFields: listFields,
                    min: min,
                    max: max,
                    detail: detail,
                  },
                );
              });
          var renderAddonOptions = {
            add: add,
            remove: remove,
            move: move,
            listFields: listFields,
            min: min,
            max: max,
            detail: detail,
            errors: errors,
            warnings: warnings,
          };
          return /*#__PURE__*/ React.createElement(
            React.Fragment,
            null,
            /*#__PURE__*/ React.createElement(
              'div',
              {
                className: 'szzj-field-list-items-addon-before',
              },
              renderListItemsAddonBefore ? renderListItemsAddonBefore(renderAddonOptions) : null,
            ),
            /*#__PURE__*/ React.createElement(
              'div',
              {
                className: 'szzj-field-list-items',
              },
              listFieldsNode,
            ),
            renderListItemsAddon(renderAddonOptions),
          );
        },
      ),
      footer &&
        /*#__PURE__*/ React.createElement(
          'div',
          {
            className: 'szzj-field-list-footer',
          },
          footer,
        ),
    ),
  );
}
export default FieldList;
